/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2008
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DNNContentBuilder.UserControls
{
    /// <summary>
    ///		Summary description for FormSettings.
    /// </summary>
    public class FormletUploaderControl : DotNetNuke.Entities.Modules.PortalModuleBase
    {
        #region Constants
        public const string resource = "~/DesktopModules/DNNContentBuilder/App_LocalResources/FormletUploaderControl";
        #endregion

        #region Controls
        protected LinkButton btUpdate;
        protected LinkButton btCancel;
        protected System.Web.UI.HtmlControls.HtmlInputFile fileUpload;
        protected System.Web.UI.HtmlControls.HtmlTableRow trFormlets;
        protected Literal ltMessage;
        protected DataList dlFormlets;
        protected Literal ltLabel;
        #endregion

        #region Member Variables
        private readonly ControlTypeController ctController = new ControlTypeController();
/*
        private   ControlTypeInfo ctInfo = new ControlTypeInfo();
*/
        #endregion

        #region Event Handlers
        private void Page_Load(object sender, EventArgs e)
        {
            this.btUpdate.Text = Commons.GetLocalizedString("button_Upload", resource);
            this.btCancel.Text = Commons.GetLocalizedString("button_Return", resource);
            this.ltLabel.Text  = Commons.GetLocalizedString("label_Upload", resource);
			
            this.dlFormlets.Visible = true;
            this.btUpdate.Visible = true;
            this.fileUpload.Visible = true;
            this.trFormlets.Visible = true;
			
            ShowFormlets();

        }
		
        private void ShowFormlets() {
            ArrayList formlets = ctController.GetAllControlType();

            // Exclude 'System' formlets from list
            for (int loop = formlets.Count - 1; loop > -1; loop--)
            {
                ControlTypeInfo ctInfo = formlets[loop] as ControlTypeInfo;

                if (ctInfo != null)
                    if (ctInfo.IsPrimaryKey || ctInfo.IsLinker) 
                    {
                        formlets.RemoveAt(loop);
                    }
            }

            this.dlFormlets.DataSource     = formlets;
            this.dlFormlets.DataBind();
        }
	
        private void btCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
		
        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btUpdate.Click += new System.EventHandler(this.btUpdate_Click);
            this.btCancel.Click += new System.EventHandler(this.btCancel_Click);
            this.Load += new System.EventHandler(this.Page_Load);
            this.dlFormlets.ItemCreated += new DataListItemEventHandler(dlFormlets_ItemCreated);

        }
        #endregion

        private void btUpdate_Click(object sender, EventArgs e)
        {
            this.dlFormlets.Visible = false;
            this.btUpdate.Visible = false;
            this.fileUpload.Visible = false;
            this.trFormlets.Visible = false;
            this.ltLabel.Text = "";				
				
            Unzip unzip = new Unzip();

            try 
            {
                ltMessage.Text = unzip.Execute(fileUpload.PostedFile.InputStream, this);
            }
            catch (Exception error) 
            {
                ltMessage.Text = error.Message;
            }

            DotNetNuke.Common.Globals.NavigateURL(this.TabId,"");
        }

        private void dlFormlets_ItemCreated(object sender, DataListItemEventArgs e)
        { 
            ListItemType elemType = e.Item.ItemType;

            if (elemType == ListItemType.Item || elemType == ListItemType.AlternatingItem)
            { 
                Control btExcludeFormlet = e.Item.FindControl("btExcludeFormlet"); 

                ArgumentValidator.CheckForNullReference(btExcludeFormlet, "btExcludeFormlet");
                ArgumentValidator.CheckForExpectedType(btExcludeFormlet, "btExcludeFormlet", typeof(ImageButton));

                ImageButton imageButton = btExcludeFormlet as ImageButton;
				
                ((ImageButton)btExcludeFormlet).Attributes.Add("onClick", "javascript:return confirm('" + Commons.GetLocalizedString("label_ConfirmDelete", resource) + "')");

                if (imageButton != null)
                    imageButton.Click += btExcludeFormlet_OnClick;
            }
        }
		
        private void btExcludeFormlet_OnClick(object sender, ImageClickEventArgs e)
        {
            ArgumentValidator.CheckForNullReference(sender, "sender");
            ArgumentValidator.CheckForExpectedType(sender, "sender", typeof(ImageButton));

            ImageButton bdFormletToExclude = (ImageButton)sender;
			
            ControlTypeInfo ctInfo = new ControlTypeInfo();
			
            using (ControlTypeController ctController = new ControlTypeController())
            {
                ctInfo = ctController.GetItem(Int32.Parse(bdFormletToExclude.CommandArgument));
                ctController.Delete(ctInfo.ControlTypeId);
            }
			
            ShowFormlets();
        }
		
    }
}